# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_with_rust="yes"
if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
  # rust doesn't support aarch64-pc-windows-gnu (msys2/CLANG-packages#28)
  # and it apparently hardcodes libgcc (msys2/CLANG-packages#29), so disable
  # it in all clang prefixes
  _with_rust="no"
fi

_realname=cryptography
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.4.4
pkgrel=1
pkgdesc="A package designed to expose cryptographic recipes and primitives to Python developers (mingw-w64)"
url='https://pypi.org/project/cryptography/'
license=('Apache')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-python-cffi"
         "${MINGW_PACKAGE_PREFIX}-python-idna")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
if [ "${_with_rust}" == "yes" ]; then
  makedepends+=("${MINGW_PACKAGE_PREFIX}-python-setuptools-rust")
else
  export CRYPTOGRAPHY_DONT_BUILD_RUST=1
fi
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest-runner"
              #"${MINGW_PACKAGE_PREFIX}-python-iso8601"
              "${MINGW_PACKAGE_PREFIX}-python-pretend"
              #"${MINGW_PACKAGE_PREFIX}-python-cryptography-vectors"
              "${MINGW_PACKAGE_PREFIX}-python-hypothesis"
              "${MINGW_PACKAGE_PREFIX}-python-pytz")
source=(https://pypi.io/packages/source/c/cryptography/${_realname}-${pkgver}.tar.gz
        0001-update-pyo3.patch
        0002-move-setuptools-rust-import.patch)
sha256sums=('ee5e19f0856b6fbbdbab15c2787ca65d203801d2d65d0b8de6218f424206c848'
            '07a9af241298fb19353a4329de779865a413cd2e9f041b12b49e2998bf6ab933'
            '7374d57ae28eefe78394f2d6d983adf7d216c040c3b4bac8e83e4f5132c51fa2')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/PyO3/pyo3/pull/1423
  patch -Np1 -i "${srcdir}"/0001-update-pyo3.patch
  # don't fail for lack of setuptools-rust if not building rust
  patch -Np1 -i "${srcdir}"/0002-move-setuptools-rust-import.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # ${MINGW_PREFIX}/bin/python setup.py pytest
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
